﻿using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LaserScan
{
    /// <summary>
    /// 文件操作类
    /// </summary>
    class FileOperation
    {
        /// <summary>
        /// 选择文件夹路径
        /// </summary>
        /// <returns></returns>
        public static string OpenFolder()
        {
            string folderPath = "";
            var folder = new FolderBrowserDialog();
            if (folder.ShowDialog() == DialogResult.OK)
            {
                folderPath = folder.SelectedPath;
            }
            return folderPath;
        }
        /// <summary>
        /// 获取文件的文件夹路径
        /// </summary>
        /// <param name="fullPath">文件全路径</param>
        /// <returns></returns>
        public static string GetFilePath(string fullPath)
        {
            /*2015.9.16修改*/
            try
            {
                string fileDirName = Path.GetDirectoryName(fullPath);
                return fileDirName;
            }
            catch
            {
                return null;
            }

        }
        /// <summary>
        /// 获取文件后缀名
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public static string GetFilExtend(string FileName)
        {
            return FileName.Substring(FileName.LastIndexOf(".") + 1);
        }
        /// <summary>
        /// 获取带后缀名的文件名
        /// </summary>
        /// <param name="fullPath">文件完整路径</param>
        /// <returns></returns>
        public static string GetFileName(string fullPath)
        {
            /*2015.9.16修改*/
            try
            {
                string fileName = Path.GetFileName(fullPath);
                return fileName;
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 获取不带后缀名的文件名
        /// </summary>
        /// <param name="fullPath">文件完整路径</param>
        /// <returns></returns>
        public static string GetFileNameNoExtend(string fullPath)
        {
            /*2015.9.16修改*/
            try
            {
                string fileName = Path.GetFileNameWithoutExtension(fullPath);
                return fileName;
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 文件复制
        /// </summary>
        /// <param name="srcdir">原路径</param>
        /// <param name="desdir">目标路径</param>
        public static void CopyFile(string srcdir, string desdir)
        {
            if (desdir.LastIndexOf("\\") != (desdir.Length - 1))
            {
                desdir = desdir + "\\";
            }
            string[] filenames = Directory.GetFileSystemEntries(srcdir);
            foreach (string file in filenames)// 遍历所有的文件和目录
            {
                if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                {

                    string currentdir = desdir + file.Substring(file.LastIndexOf("\\") + 1);
                    if (!Directory.Exists(currentdir))
                    {
                        Directory.CreateDirectory(currentdir);
                    }

                    CopyFile(file, currentdir);
                }
                else // 否则直接copy文件
                {
                    string srcfileName = file.Substring(file.LastIndexOf("\\") + 1);

                    srcfileName = desdir + srcfileName;

                    if (!Directory.Exists(desdir))
                    {
                        Directory.CreateDirectory(desdir);
                    }

                    File.Copy(file, srcfileName);
                }
            }
        }
        /// <summary>
        /// 选择文件
        /// </summary>
        /// <param name="multiselect"></param>
        /// <param name="filter"></param>
        /// <param name="title"></param>
        /// <param name="initialDirectory"></param>
        /// <returns>返回选择文件的完整路径</returns>
        public static string[] OpenFile(string filter, string title, string initialDirectory, bool multiselect)
        {
            string[] filePath;
            var openDialog = new OpenFileDialog();
            openDialog.Multiselect = multiselect;
            openDialog.Title = title;
            openDialog.InitialDirectory = initialDirectory;
            openDialog.Filter = filter;
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                filePath = openDialog.FileNames;
            }
            else
                filePath = null;
            return filePath;
        }
        /// <summary>
        /// 打开文件
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="title"></param>
        /// <param name="initialDirectory"></param>
        /// <returns></returns>
        public static string OpenFile(string filter, string title, string initialDirectory)
        {
            string filePath;
            var openDialog = new OpenFileDialog();
            openDialog.Multiselect = false;
            openDialog.Title = title;
            openDialog.InitialDirectory = initialDirectory;
            openDialog.Filter = filter;
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                filePath = openDialog.FileName;
            }
            else
                filePath = null;
            return filePath;
        }
        /// <summary>
        /// 保存为文件
        /// </summary>
        /// <param name="filter">文件格式过滤</param>
        /// <param name="title">保存文件对话框标题</param>
        /// <param name="initialDirectory">原始路径</param>
        /// <returns></returns>
        public static string SaveFile(string filter, string title, string initialDirectory)
        {
            string filePath;
            var saveDialog = new SaveFileDialog();
            saveDialog.Title = title;
            saveDialog.InitialDirectory = initialDirectory;
            saveDialog.Filter = filter;
            if (saveDialog.ShowDialog() == DialogResult.OK)
            {
                filePath = saveDialog.FileName;
            }
            else
                filePath = null;
            return filePath;
        }
        /// <summary>
        /// 获取制定格式的文件
        /// </summary>
        /// <param name="FileValue"></param>
        /// <param name="Format"></param>
        /// <returns></returns>
        public static string FindFile(string FileValue, string Format)
        {
            if (Directory.Exists(FileValue))
            {
                string[] FileNames = Directory.GetFileSystemEntries(FileValue);
                foreach (string FileName in FileNames)
                {
                    if (GetFilExtend(FileName) == Format)
                    {
                        return FileName;
                    }
                }
            }
            else if (File.Exists(FileValue))
            {
                if (GetFilExtend(FileValue) == Format)
                    return FileValue;
                else
                    return null;
            }
            return null;
        }

        /// <summary>
        /// 获取扫描文件的序号
        /// </summary>
        /// <param name="FullPath"></param>
        /// <returns></returns>
        public static int FindFileNum(string FullPath)
        {
            try
            {
                string FileNameNoExtend = Path.GetFileNameWithoutExtension(FullPath);
                int Num = FileNameNoExtend.Length;
                int FileNum = Convert.ToInt32(FileNameNoExtend.Substring(Num - 3, 3));
                return FileNum;
            }
            catch
            {
                return -1;
            }
        }

    }

}
